import React from 'react';

class ErrorPage extends React.Component {
  state = {
    hasError: false,
    a: 1,
  };
  // 先触发
  static getDerivedStateFromError(error) {
    console.log(error, 'getDerivedStateFromError');
    return {
      hasError: true,
    };
  }

  // 后触发
  componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void {
    console.log(error, errorInfo, 'componentDidCatch');
  }

  render(): React.ReactNode {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return <h1>错误触发了....</h1>;
    }

    return this.props.children;
  }
}

export default ErrorPage;
